package net.wanho.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.mapper.ProductTypeMapper;
import net.wanho.po.ProductType;
import net.wanho.service.ProductTypeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 32033
 * @description 针对表【product_type(商品分类表)】的数据库操作Service实现
 * @createDate 2025-01-11 17:01:23
 */
@Service
@RequiredArgsConstructor
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType>
        implements ProductTypeService {
    private final RedisTemplate<String, Object> redisTemplate;
    private final ProductTypeMapper productTypeMapper;

    @Override
    public List<ProductType> getProductCategoryTree() {
        List<ProductType> productTypeList = this.list();

        List<ProductType> list = productTypeList.stream()
                .filter(item -> item.getParentId() == 0)
                .map(item -> {
                    // 递归查询子权限
                    findChildren(item, productTypeList);
                    return item;
                })
                .collect(Collectors.toList());
        return list;
    }

    private void findChildren(ProductType item, List<ProductType> authInfoList) {
        List<ProductType> children = authInfoList.stream()
                .filter(subItem -> subItem.getParentId().equals(item.getTypeId()))
                .map(subItem -> {
                    findChildren(subItem, authInfoList);
                    return subItem;
                }).collect(Collectors.toList());
        item.setChildProductCategory(children);
    }
}




